//indent-list.js
//获取应用实例  
var app = getApp()
Page({
    data: {
        /**
         * 页面配置
         */
        siteInfo: app.siteInfo,
        winWidth: 0,
        winHeight: 0,
    },
    onLoad: function () {
        var that = this;
        app.initPage({'navigationBarTitleText': '订单详情'});
        var order_id = that.options.id;
        that.setData({
            'id': order_id,
        });

        app.util.request({
            'url': 'order/orderDetail',
            'data': {id: order_id},
            'cachetime': '0',
            'showLoading': true,
            success: function (res) {
                var data = res.data.data;
                that.setData({
                    'showLoading': 0,
                    'data': data,
                });
            }
        })
    },
    //取消订单
    cancelOrder: function (e) {
        var that = this;
        const order_id = that.data.id;
        wx.showModal({
            content: '这么好的宝贝，确定不要了吗？',
            success: function (res) {
                if (res.confirm) {
                    app.util.request({
                        'url': 'order/cancelOrder',
                        'showLoading': false,
                        'data': {'order_id': order_id},
                        success: function (res) {
                            wx.showToast({
                                title: '取消订单成功',
                                icon: 'none',
                                success: function () {
                                    var page = getCurrentPages().pop();
                                    page.onLoad();
                                }
                            });
                        },
                        fail: (res) => {
                            wx.showToast({
                                title: '网络或服务器异常',
                            })
                        },
                    });
                } else if (res.cancel) {
                }
            }
        })
    },
    //删除订单
    delOrder: function (e) {
        var that = this;
        const order_id = that.data.id;
        wx.showModal({
            content: '确定删除该订单吗？删除了的订单不可恢复',
            success: function (res) {
                if (res.confirm) {
                    app.util.request({
                        'url': 'order/delOrder',
                        'showLoading': false,
                        'data': {'order_id': order_id},
                        success: function (res) {
                            wx.showToast({
                                title: '删除订单成功',
                                icon: 'none',
                                success: function () {
                                    wx.redirectTo({
                                        url: '/wxz_superstore/pages/order-list/order-list?order_status=all',
                                    });
                                }
                            });
                        },
                        fail: (res) => {
                            wx.showToast({
                                title: '网络或服务器异常',
                            })
                        },
                    });
                } else if (res.cancel) {
                }
            }
        })
    },
    //订单付款
    gotoPay: function (e) {
        var that = this;
        const order_id = that.data.id;
        var param = {};
        param.order_id = order_id;
        app.util.getUserInfo(() => {
            //请求支付接口
            app.util.request({
                'url': 'entry/wxapp/pay', //调用wxapp.php中的doPagePay方法获取支付参数
                data: param,
                'cachetime': '0',
                success(res) {
                    if (res.data && res.data.data && !res.data.errno) {
                        //发起支付
                        wx.requestPayment({
                            'timeStamp': res.data.data.timeStamp,
                            'nonceStr': res.data.data.nonceStr,
                            'package': res.data.data.package,
                            'signType': 'MD5',
                            'paySign': res.data.data.paySign,
                            'success': function (res) {
                                //执行支付成功提示
                                wx.showToast({
                                    title: '支付成功',
                                    icon: 'success',
                                    success: function () {
                                        var page = getCurrentPages().pop();
                                        page.onLoad();
                                    }
                                });
                            },
                            'fail': function (res) {
                                if (res.errMsg == 'requestPayment:fail cancel') {
                                    wx.showToast({
                                        title: '您已取消支付',
                                        icon: 'none',
                                    });
                                }
                            }
                        })
                    }
                },
                fail(res) {
                    wx.showModal({
                        title: '系统提示',
                        content: res.data.message ? res.data.message : '错误',
                        showCancel: false,
                        success: function (res) {
                            if (res.confirm) {
                                // backApp();
                            }
                        }
                    })
                }
            })
        });
    },
    //确认收货
    confirmation: function (e) {
        var that = this;
        const order_id = that.data.id;
        wx.showModal({
            content: '确定已经收到宝贝了吗？',
            success: function (res) {
                if (res.confirm) {
                    app.util.request({
                        'url': 'order/confirmationOrder',
                        'showLoading': false,
                        'data': {'order_id': order_id},
                        success: function (res) {
                            wx.showToast({
                                title: '收货成功',
                                icon: 'none',
                                success: function () {
                                    var page = getCurrentPages().pop();
                                    page.onLoad();
                                }
                            });
                        },
                        fail: (res) => {
                            wx.showToast({
                                title: '网络或服务器异常',
                            })
                        },
                    });
                } else if (res.cancel) {
                }
            }
        })
    }
})  